const { Controller } = require('egg');

class AdminController extends Controller {
  // 查看数据
  async index() {
    const { ctx } = this;
    const list = await ctx.service.admin.getAdminList();
    if (list) {
      ctx.body = {
        code: 200,
        msg: '查询成功',
        data: list,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '数据库查询失败',
      };

    }
  }
  // 增加数据
  async create() {
    try {
      const data = this.ctx.request.body;
      const list = await this.ctx.service.admin.addAdminList(data);
      if (list) {
        this.ctx.body = {
          code: 200,
          msg: '添加管理员成功',
          data: list,
        };
      } else {
        this.ctx.body = {
          code: 500,
          msg: '添加管理员失败',
        };
      }
    } catch (error) {
      return error;
    }
  }
  // 根据id删除管理员
  async delete() {
    try {
      const adminId = this.ctx.params.id;
      const result = await this.ctx.service.admin.delAdmin(adminId);
      this.ctx.body = {
        code: 200,
        msg: result,
      };
    } catch (error) {
      return error;
    }
  }
  // 根据id修改管理员信息
  async update() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    const data = ctx.request.body;
    // console.log(data);
    const updateRes = await service.admin.updateAdmin(data, id);
    // console.log('sssss', updateRes);
    this.ctx.body = {
      code: 200,
      msg: '管理员信息修改成功',
      data: updateRes,
    };
  }
}


module.exports = AdminController;
